# -*- coding: utf-8 -*-
'''
Created on 25/set/2009

@author: sax
'''

from django.core.urlresolvers import reverse, resolve, NoReverseMatch

from django.template.loader import get_template
from django.utils.safestring import mark_safe
from django.template.context import Context
import logging



class XActionButton(object):
    def __init__(self, action_index, object_id, label):
        self.action_index = action_index
        self.object_id = object_id
        self.label = label
        
    def __str__(self):
        link = "op/%s" % self.action_index
        return '<a href="%s">%s</a>' % (link, self.label)

    def __unicode__(self):
        return mark_safe(unicode(self.__str__()))
        
class XModelAdminButton2(object):
    template = 'xadmin/inlines/ui_button.html'

    def __str__(self):
        
        ctx =  {'label': self.label, 
                'url' : self.link,
                'css' : ("ui-icon " + self.css) if self.css else ""
            }        
        t = get_template( self.template )
        return  t.render(Context(ctx)) 
    
    def __unicode__(self):
        return mark_safe(unicode(self.__str__()))
    
class XModelAdminButton(object):
    template = 'xadmin/inlines/ui_button.html'
    
    def __init__(self, label=None, link=None, css=None, named_url=None, view=None, callback=None):
        self.named_url = named_url
        self.callback = callback
        self.view = view
        self.label = label
        self.link = link
        self.css = css # is a css style 
    
#    def get_url_name(self, xmodel_admin):
#        info = xmodel_admin.model._meta.app_label, xmodel_admin.model._meta.module_name
        
    def config(self, request, object_id=None, xmodel_admin=None):
        
        if self.named_url:
            _args = [object_id] if object_id else None
            
            try:
                self.link = reverse("%s:%s" % (xmodel_admin.admin_site.name, self.named_url),
                                args=_args)
                
            except NoReverseMatch, e:
                # here if do not pass object_id, so the button is only for edit page
                # should we check if there's some other error checking for the function
                #  without object_id parameter ?   
                #reverse("%s:%s" % (xmodel_admin.admin_site.name, self.named_url), args=[1])                
                logging.debug("ERROR!!: " % e)
                return "" 
            
            self.view,a,b = resolve(self.link)            

        
        if self.label is None:
            self.label = getattr(self.view, 'label', self.view.__name__)
        if self.css is None:
            self.css = getattr(self.view, 'css', "")
        self.link = "%s?%s" % (self.link, request.META['QUERY_STRING'])
        
        return self
    
    def __str__(self):
        
        ctx =  {'label': self.label, 
                'url' : self.link,
                'css' : ("ui-icon " + self.css) if self.css else ""
            }        
        t = get_template( self.template )
        return  t.render(Context(ctx)) 
    
    def __unicode__(self):
        return mark_safe(unicode(self.__str__()))
    
